﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.DataProvider;
using ERP.Manager.DataEntity;
using ERP.Manager.DataHelper;
using ERP.Manager.Core;
using System.Data;
using System.Data.SqlClient;

namespace ERP.Manager.SQLServerProvider
{
    public class DepartmentRepository : IDepartmentRepository
    {
        #region 新增部门信息的方法
        public int addDepartment(DepartmentInfo department)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_DEPARTMENT,
                new SqlParameter("@departmentId", department.departmentId),
                new SqlParameter("@departmentName", department.departmentName),
                new SqlParameter("@departmentnote", department.departmentNote));
            return rowcount;
        }
        #endregion

        #region 修改部门信息的方法
        public int UpdateDepartment(DepartmentInfo department)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_DEPARTMENT,
                new SqlParameter("@departmentName", department.departmentName),
                new SqlParameter("@departmentId", department.departmentId),
                 new SqlParameter("@departmentnote", department.departmentNote));
            return rowcount;
        }
        #endregion

        #region 查询所有部门信息的方法
        public List<DepartmentInfo> getDepartments()
        {
            List<DepartmentInfo> list = new List<DepartmentInfo>();
            DepartmentInfo department = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_DEPARTMENT);
            try
            {
                while (read.Read())
                {
                    department = new DepartmentInfo();
                    department.id = int.Parse(read["id"].ToString());
                    department.departmentId = read["departmentId"].ToString();
                    department.departmentName = read["departmentName"].ToString();
                    department.departmentNote = read["departmentNote"].ToString();
                    list.Add(department);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 根据编号查询部门的详细信息
        public DepartmentInfo DetailDepartment(string departmentid)
        {
            DepartmentInfo departments = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.DETAIL_DEPARTMETN, new SqlParameter("@departmentId", departmentid));
            try
            {
                if (read.Read())
                {
                    departments = new DepartmentInfo();
                    departments.id = int.Parse(read["id"].ToString());
                    departments.departmentId = read["departmentId"].ToString();
                    departments.departmentName = read["departmentName"].ToString();
                    departments.departmentNote = read["departmentnote"].ToString();
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return departments;
        }
        #endregion
    }
}
